package com.gzqylc.docker.extend.auth.init;

import com.gzqylc.docker.extend.auth.dao.BaseUserDao;
import com.gzqylc.docker.extend.auth.domain.BaseUser;
import com.gzqylc.lang.tool.MD5Tool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * @Desc： 系统初始用户加载器
 * @ClassName SystemUserInit
 * @Author 孔晓文
 * @Date 2022/2/21 上午11:12
 */
@Component
@Slf4j
public class SystemUserInit implements ApplicationRunner {


    @Autowired
    BaseUserDao baseUserDao;

    public static final String DEFAULT_LOGIN_NAME = "qylcDocker";

    public static final String DEFAULT_USER_ID = "0";

    public static final String DEFAULT_USER_PASSWORD = "qylcDocker!@#";


    @Override
    public void run(ApplicationArguments args) throws Exception {

        BaseUser dbSystemUser = baseUserDao.findOneByField(
                BaseUser.Fields.loginName, DEFAULT_LOGIN_NAME
        );
        if (dbSystemUser != null) {
            return;
        }
        log.info("开始初始化默认用户");
        //不存在用户 初始化用户
        BaseUser initUser = new BaseUser();
        initUser.setUserName("默认超级管理员用户");
        initUser.setLoginName(DEFAULT_LOGIN_NAME);
        initUser.setPassWord(MD5Tool.md5(DEFAULT_USER_PASSWORD));
        initUser.setEmailAddress("");
        initUser.setId(DEFAULT_USER_ID);
        baseUserDao.save(initUser);
        log.info("初始化默认用户成功");
    }
}
